package component.controls.group
{
	import component.controls.Box;
	import component.controls.RadioButton;
	import component.controls.ui.Component;
	import component.global.ComponentSetting;
	import component.global.consts.Directions;
	import component.global.manager.EventMgr;
	import component.global.utils.DisplayUtils;
	import component.global.utils.ObjectUtil;
	
	import flash.events.MouseEvent;
	
	public class RadioButtonGroup extends Component
	{
		public function RadioButtonGroup(direction:String = Directions.HORIZONTAL,spacing:int = ComponentSetting.BOX_DEFAULT_SPACING)
		{
			_direction = direction;
			_spacing = spacing;
			super();
		}
		
		protected var _direction:String;
		protected var _spacing:int;
		protected var _items:Vector.<RadioButton>;
		protected var _box:Box;
		protected var _selectedIndex:int = -1;
		
		override protected function init():void{
			_items = new Vector.<RadioButton>();
			_box = new Box(_direction,_spacing);
			
			DisplayUtils.addDisplayObject(this,_box);
		}
		
		public function get selectedIndex():int{
			return _selectedIndex;
		}
		
		public function set selectedIndex(value:int):void{
			if(_selectedIndex == value)return;
			_selectedIndex = value;
			for (var i:int = 0; i < _items.length; i++) 
			{
				_items[i].selected = false;
			}
			_items[_selectedIndex].selected = true;
			
		}
		
		public function get selectedItem():RadioButton{
			return _items[_selectedIndex];
		}
		
		public function addItem(label:String):void{
			var btn:RadioButton = new RadioButton(label,false);
			EventMgr.Instance.addEvent(btn,[MouseEvent.CLICK],[__onItemClickHandler]);
			_items.push(btn);
			_box.addContent(btn);
		}
		
		protected function __onItemClickHandler(e:MouseEvent):void{
			var btn:RadioButton = e.currentTarget as RadioButton;
			selectedIndex = _items.indexOf(btn);
		}
		
		override public function dispose():void{
			super.dispose();
			ObjectUtil.dispose(_box);_box=null;
			ObjectUtil.disposeElements(_items);_items = null;
		}
	}
}